
export default class ResLoadHelper {

    static load(path: string, type: typeof cc.Asset, callback: Function = null, bundleName: string) {
        let bundle = cc.assetManager.getBundle(bundleName)
        if (bundle) {
            bundle.load(path, type, (err, asset) => {
                if (err) {
                    callback && callback(err, null)
                } else {
                    callback && callback(null, asset)
                }
            })

        } else {
            cc.assetManager.loadBundle(bundleName, (err, bundle:cc.AssetManager.Bundle) => {
                if (bundle) {
                    this.load(path, type, callback, bundleName)
                } else {
                    callback && callback(err, null)
                }
            })
        }
    }

    public static loadResSync(path: string, bundleName: string, type: typeof cc.Asset) {
        return new Promise((resolve, rejcet) => {
            this.load(path, type, (e, res) => {
                if (e) {
                    resolve(null);
                    return
                }
                resolve(res);
            }, bundleName)
        })
    }


    static setSpriteFrame(sp: cc.Sprite, path: string, bundle: string, callback = null): void {
        let loader = cc.assetManager.getBundle(bundle)
        loader.load(path, cc.SpriteFrame, (error, assets: cc.SpriteFrame) => {
            if (error) {
                cc.log('error', path)
                return
            }
            sp.spriteFrame = assets
            callback && callback()
        })
    }

}